#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtSql>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QSqlQuery>
#include <QBuffer>
#include <QDateTime>
#include <QScreen>
#include <QFile>

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow){
    ui->setupUi(this);
//    调用自定义方法
    insertAndQueryImage();
}

MainWindow::~MainWindow(){
    delete ui;
}

void MainWindow::insertAndQueryImage(){

    QSqlDatabase db = MainWindow::connDatabae();
    QSqlQuery query = QSqlQuery(db);
    // 生成图片名称
    QString imageName = QDateTime::currentDateTime().toString("yyyyMMddHHmmss")+".jpg";
    // 截取当前屏幕并保存为jpg格式
    QPixmap inPixmap = screen->grabWindow(0);
    QByteArray inByteArray;
    QBuffer inBuffer(&inByteArray);
    inBuffer.open(QIODevice::WriteOnly);
    inPixmap.save(&inBuffer,"jpg");
    inBuffer.close();

    query.prepare("insert into test_qt_lob(photo_bytea,photo_text)values(:imagedata,:imagename)");
    query.bindValue(":imagedata",inByteArray);
    query.bindValue(":imagename",imageName);

    if(!query.exec()){
        qDebug() << "图片插入失败:\n" << query.lastError();
        exit(0);
    }else {
        qDebug() << "插入成功！";
    }

    // 从数据库读取数据
    if(!query.exec("select photo_bytea,photo_text from test_qt_lob where photo_text = '"+imageName+"'")){
        qDebug() << "获取图片数据失败:\n" << query.lastError();
        exit(0);
    }
    query.first();
    QByteArray outByteArray = query.value(0).toByteArray();
    QString imgName = query.value(1).toString();
    qDebug() << "展示的图片名称：" << imgName;
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData(outByteArray);
    db.close();
    // 展示刚保存的图片
    QLabel *label = new QLabel(this);
    label->setText(imgName);
    label->setPixmap(outPixmap);
    label->setGeometry(0,0,screen->grabWindow(0).width(),screen->grabWindow(0).height());
    label->setScaledContents(true);

}

void MainWindow::insertImage(){

    QSqlDatabase db = MainWindow::connDatabae();
    QSqlQuery query;
    QString path = "D:/Qt/QtConnHGdbByODBC/test.png";//图片的路径
    QString imageName = QDateTime::currentDateTime().toString("yyyyMMddHHmmss")+".png";
    QFile * file = new QFile(path);
    // 设置为只读模式
    file->open(QIODevice::ReadOnly);
    // 将数据转换为QByteArray类型存储到数据库中
    QByteArray data = file->readAll();
    file->close();
    QString sql = "insert into test_qt_lob(photo_bytea,photo_text)values(:imagedata,:imagename)";
    query.prepare(sql);
    query.bindValue(":imagedata",data);
    query.bindValue(":imagename",imageName);
    if(!query.exec()){
        printf("insert error");
        // 若不成功，打印错误
        printf(query.lastError().text().toLocal8Bit().data());
        printf("n");
    }else{
        printf("插入成功！");
        printf("n");
    }
    db.close();
}

/*查询图片并展示*/
void MainWindow::queryShowImage(){

    QSqlDatabase db = MainWindow::connDatabae();
    QSqlQuery query = db.exec("select photo_bytea,photo_text from test_qt_lob");
    query.first();
    QByteArray outByteArray = query.value(0).toByteArray();
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData(outByteArray);
    db.close();
    // 标签展示图片
    QLabel *label = new QLabel(this);
    label->setPixmap(outPixmap);
    label->setGeometry(0,0,screen->grabWindow(0).width(),screen->grabWindow(0).height());
    label->setScaledContents(true);

}

QSqlDatabase MainWindow::connDatabae(){

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    QString strconn = QString("Driver={PostgreSQL Unicode(x64)};SERVER=192.168.21.138;PORT=5870;DATABASE=test;UID=test;PWD=hg123456;");
    db.setDatabaseName(strconn);
    if (!db.open()){
        qDebug() <<"数据库连接失败:\n" << db.lastError().text();
        exit(0);
    }else{
        qDebug() << "数据库连接成功！";
    }

    return db;
}
